// 引入接口地址
import { specsList, specsCount } from "../../../request/api"
// 商品规格仓库模块
const state = {
    specsList: [],
    total: 0,
    size: 3,
    page: 1
}

const mutations = {
    REQ_SPECSLIST(state, payload) {
        return state.specsList = payload
    },
    REQ_COUNT(state, payload) {
        return state.total = payload
    },
    CHANGE_PAGE(state, payload) {
        return state.page = payload
    }
}

const actions = {
    specsList_Action(context) {
        // 调用商品规格列表接口
        specsList({
            size: context.state.size,
            page: context.state.page
        }).then(res => {
            console.log(res);
            if (res.code == 200) {
                let list = res.list || []
                // 判断除第一页外当前页是否还有管理员数据
                if (context.state.page != 1 && list.length == 0) {
                    // 调用改变页数行动
                    context.dispatch("changePage_Action", context.state.page - 1)
                }
                context.commit("REQ_SPECSLIST", res.list)
            }
        })
    },
    specsCount_Action({ commit }) {
        // 调用商品规格总数接口
        specsCount().then(res => {
            if (res.code == 200) {
                commit("REQ_COUNT", res.list[0].total)
            }
        })
    },
    changePage_Action(context, payload) {
        // 点击切换选中页面
        context.commit("CHANGE_PAGE", payload)
        // 调用渲染商品规格列表行动
        context.dispatch("specsList_Action")
    }
}

const getters = {
    // 获取商品规格列表
    getSpecsList(state) {
        return state.specsList;
    },
    // 获取商品规格总数
    getTotal(state) {
        return state.total;
    },
    // 获取页数展示条数
    getSize(state) {
        return state.size
    }
}

// 导出菜单仓库
export default {
    state,
    mutations,
    actions,
    getters,
    namespaced: true
}